package com.rd.student.jike.class2.xuezhixin.Student7_12;

import java.util.*;

public class StudentWork {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();
        int studentNum = 0;
        while (true){
            System.out.println("------学生信息管理系统------");
            System.out.println("0、查看所有小组成员");
            System.out.println("1、新增小组成员");
            System.out.println("2、移除小组成员");
            System.out.println("3、根据班级查询成员列表");
            System.out.println("4、根据专业查询成员列表 ");
            System.out.println("5、根据年龄查询成员列表 ");
            System.out.println("6、返回所有成员名字");
            System.out.println("7、计算成员的平均年龄");
            System.out.println("8、根据年龄范围返回成员列表");
            System.out.println("9、退出");

            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();
            switch (num){
                case 0:
                    ToolMain.inqire(arrayList);
                    break;
                case 1:
                    studentNum++;
                    ToolMain.addPeople(arrayList,studentNum);
                    break;
                case 2:
                    ToolMain.removePeople(arrayList);
                    break;
                case 3:
                    ToolMain.inquireCls(arrayList);
                    break;
                case 4:
                    ToolMain.inquireMajor(arrayList);
                    break;
                case 5:
                    ToolMain.inquireAge(arrayList);
                    break;
                case 6:
                    ToolMain.returnName(arrayList);
                    break;
                case 7:
                    ToolMain.avarageAge(arrayList);
                    break;
                case 8:
                    ToolMain.radiusAge(arrayList);
                    break;
                case 9:
                    return;
                default:
                    System.out.println();
            }
        }
    }
}
//实现的工具类
class ToolMain{
    //0.查看所有小组成员
    public static void inqire(ArrayList arrayList){
        if(arrayList.isEmpty()){
            System.out.println("暂未添加小组成员");
        }else {
            System.out.println(arrayList);
        }
    }

    //1.新增小组成员
    public static void addPeople(ArrayList arrayList,int studentNum){
        StudentMain student  =new StudentMain();
        Scanner scanner = new Scanner(System.in);
        //把列表成员的名字导入新列表
        ArrayList list = new ArrayList<>();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain student1 = (StudentMain)arrayList.get(i);
            list.add(student1.getName());
        }

        System.out.println("姓名");
        student.setName(scanner.next());
        if(list.contains(student.getName())){
            //将输入的名字在名字列表中判断是否存在
            System.out.println("您输入的名字重复");
        }else {
            System.out.println("班级");
            student.setCls(scanner.next());
            System.out.println("专业");
            student.setMajor(scanner.next());
            System.out.println("年龄");
            student.setAge(scanner.nextInt());
            student.setDesc("2023-" + studentNum);

            arrayList.add(student);
            System.out.println("添加成功");
            inqire(arrayList);
        }
    }

    //2、移除小组成员
    public static void removePeople(ArrayList arrayList){
        Scanner scanner = new Scanner(System.in);
        //把列表成员的名字导入新列表
        ArrayList list = new ArrayList<>();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain student1 = (StudentMain)arrayList.get(i);
            list.add(student1.getName());
        }

        System.out.println("请输入您要删除的成员姓名");
        String removeName = scanner.next();
        if(list.contains(removeName)){
            for (int i = 0;i<list.size();i++){
                String s = (String) list.get(i);
                if (removeName.equals(s)){
                    arrayList.remove(i);
                    System.out.println("删除成功");
                }
            }
        }else {
            System.out.println("不存在该成员");
        }

    }

    //3、根据班级查询成员列表
    public static void inquireCls(ArrayList arrayList){
        Scanner scanner = new Scanner(System.in);
        HashMap hashMap = new HashMap();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain student = (StudentMain) arrayList.get(i);
            if(hashMap.containsKey(student.getCls())){
                int num =(int) hashMap.get(student.getCls()) + 1;
                hashMap.put(student.getCls(),num);
            }else {
                hashMap.put(student.getCls(),1);
            }
        }

        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        int a = 0;
        System.out.println("请选择您要查询的班级：");
        while (iterator.hasNext()){
            a++;
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(a + ". 班级为：" + key + " 人数为：" + value);
        }

        ArrayList list1  = new ArrayList<>();
        for (int i = 0;i<hashMap.size();i++){
            for(Object cls: hashMap.keySet()){
                list1.add(cls);
            }
        }

        int numCls = scanner.nextInt();
        if(numCls >list1.size()){
            System.out.println("您输入的选项出差");
        }else {
            String cls = (String) list1.get(numCls-1);
            for(int i = 0;i<arrayList.size();i++){
                StudentMain s2 = (StudentMain) arrayList.get(i);
                if(cls.equals(s2.getCls())){
                    System.out.println(arrayList.get(i));
                }
            }
        }
    }

    //4、根据专业查询成员列表
    public static void inquireMajor(ArrayList arrayList){
        Scanner scanner = new Scanner(System.in);
        HashMap hashMap = new HashMap();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain student = (StudentMain) arrayList.get(i);
            if(hashMap.containsKey(student.getMajor())){
                int num =(int) hashMap.get(student.getMajor()) + 1;
                hashMap.put(student.getMajor(),num);
            }else {
                hashMap.put(student.getMajor(),1);
            }
        }

        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        int a = 0;
        System.out.println("请选择您要查询的专业：");
        while (iterator.hasNext()){
            a++;
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(a + ". 专业为：" + key + " 人数为：" + value);
        }

        ArrayList list1  = new ArrayList<>();
        for (int i = 0;i<hashMap.size();i++){
            for(Object cls: hashMap.keySet()){
                list1.add(cls);
            }
        }

        int numMajor = scanner.nextInt();
        if(numMajor >list1.size()){
            System.out.println("您输入的选项出差");
        }else {
            String major = (String) list1.get(numMajor-1);
            for(int i = 0;i<arrayList.size();i++){
                StudentMain s2 = (StudentMain) arrayList.get(i);
                if(major.equals(s2.getMajor())){
                    System.out.println(arrayList.get(i));
                }
            }
        }
    }

    //5、根据年龄查询成员列表
    public static void inquireAge(ArrayList arrayList){
        Scanner scanner = new Scanner(System.in);
        HashMap hashMap = new HashMap();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain student = (StudentMain) arrayList.get(i);
            if(hashMap.containsKey(student.getAge())){
                int num =(int) hashMap.get(student.getAge()) + 1;
                hashMap.put(student.getAge(),num);
            }else {
                hashMap.put(student.getAge(),1);
            }
        }

        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        int a = 0;
        System.out.println("请选择您要查询的年龄：");
        while (iterator.hasNext()){
            a++;
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(a + ". 年龄为" + key + "岁 人数为：" + value);
        }

        ArrayList list1  = new ArrayList<>();
        for (int i = 0;i<hashMap.size();i++){
            for(Object age: hashMap.keySet()){
                list1.add(age);
            }
        }

        int numCls = scanner.nextInt();
        if(numCls >list1.size()){
            System.out.println("您输入的选项出差");
        }else {
            int age = (int) list1.get(numCls-1);
            for(int i = 0;i<arrayList.size();i++){
                StudentMain s2 = (StudentMain) arrayList.get(i);
                if(age == s2.getAge()){
                    System.out.println(arrayList.get(i));
                }
            }
        }
    }

    //6、返回所有成员名字
    public static void returnName(ArrayList arrayList){
        ArrayList list = new ArrayList<>();
        for(int i = 0;i<arrayList.size();i++){
            StudentMain s = (StudentMain) arrayList.get(i);
            list.add(s.getName());
        }
        System.out.println(list);
    }

    //7、计算成员的平均年龄
    public static void avarageAge(ArrayList arrayList){
        int ageAll =0;
        for(int i = 0;i<arrayList.size();i++) {
            StudentMain s = (StudentMain) arrayList.get(i);
            ageAll += s.getAge();
        }
        System.out.println("平均年龄为：" + ageAll/arrayList.size());
        Collections.sort(arrayList);
        System.out.println("降序排列");
        System.out.println(arrayList);
        System.out.println("升序排列");
        Collections.reverse(arrayList);
        System.out.println(arrayList);
    }

    //8、根据年龄范围返回成员列表
    public static void radiusAge(ArrayList arrayList){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择您要的年龄范围");
        System.out.println("1. 0-18");
        System.out.println("2. 18-30");
        System.out.println("3. 30-100");
        int num = scanner.nextInt();
        int a = 0;
        switch (num){
            case 1:
                for(int i = 0;i<arrayList.size();i++){
                    StudentMain s = (StudentMain) arrayList.get(i);
                    if(s.getAge() > 0 && s.getAge() <= 18){
                        System.out.println(arrayList.get(i));
                    }
                }
                if(a == 0){
                    System.out.println("没有该年龄范围成员");
                }
                break;
            case 2:
                for(int i = 0;i<arrayList.size();i++){
                    StudentMain s = (StudentMain) arrayList.get(i);
                    if(s.getAge() > 18 && s.getAge() <= 30){
                        System.out.println(arrayList.get(i));
                    }
                }
                if(a == 0){
                    System.out.println("没有该年龄范围成员");
                }
                break;
            case 3:
                for(int i = 0;i<arrayList.size();i++){
                    StudentMain s = (StudentMain) arrayList.get(i);
                    if(s.getAge() > 30 && s.getAge() <= 100){
                        System.out.println(arrayList.get(i));
                        a++;
                    }
                }
                if(a == 0){
                    System.out.println("没有该年龄范围成员");
                }
                break;
            default:
                System.out.println("您的选项错误");
        }
    }
}
//创建学生类
class StudentMain implements Comparable{
    private String name;
    private String major;
    private String cls;
    private String desc;
    private int age;

    public StudentMain() {
    }

    public StudentMain(String name, String major, String cls, String desc, int age) {
        this.name = name;
        this.major = major;
        this.cls = cls;
        this.desc = desc;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public void setCls(String cls) {
        this.cls = cls;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getMajor() {
        return major;
    }

    public String getCls() {
        return cls;
    }

    public String getDesc() {
        return desc;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int compareTo(Object o) {
        StudentMain student = (StudentMain) o;
        if (this.age > student.age) {
            return -1;
        } else if (this.age < student.age) {
            return 1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", major='" + major + '\'' +
                ", cls=" + cls +
                ", desc='" + desc + '\'' +
                ", age=" + age +
                '}' + "\n";
    }
}